/**
 * @description  格式化时间
 * @param { string } str 传入的时间字符串
 * @return { string } 返回格式化后的时间字符串
 */
export function formatDate(str: string) {
  const date = new Date(str);
  const year = date.getFullYear();
  const month = date.getMonth() + 1;
  const day = date.getDate();
  const hours = date.getHours();
  const minutes = date.getMinutes();
  const seconds = date.getSeconds();
  // 如果今年的话，就不显示年份了
  const nowDate = new Date();
  const nowYear = nowDate.getFullYear();
  const nowMonth = nowDate.getMonth() + 1;
  const nowDay = nowDate.getDate();
  const nowHours = nowDate.getHours();
  const nowMinutes = nowDate.getMinutes();
  const nowSeconds = nowDate.getSeconds();
  // 如果是一小时内的话，就显示多少分钟前
  if (
    year === nowYear &&
    month === nowMonth &&
    day === nowDay &&
    hours === nowHours
  ) {
    return `${nowMinutes - minutes}分钟前`;
  }
  // 如果是一天内的话，就显示多少小时前
  else if (year === nowYear && month === nowMonth && day === nowDay) {
    return `${nowHours - hours}小时前`;
  }
  // 如果是一月内的话，就显示多少天前
  else if (year === nowYear && month === nowMonth) {
    return `${nowDay - day}天前`;
  }
  // 如果是今年的话，就显示月份和日期
  else if (year === nowYear) {
    return `${month}-${day}`;
  }
  // 如果不是今年的话，就显示年月日
  else {
    return `${year}-${month}-${day}`;
  }
}

/**
 * @description 格式化数字
 * @param { number } num 传入的数字
 * @return { string } 返回格式化后的数字字符串
 */
export function formatNumberToString(num: number) {
  if (num < 1000) {
    return num.toString();
  } else if (num < 10000) {
    return (num / 1000).toFixed(1) + "k";
  } else {
    return (num / 10000).toFixed(1) + "w";
  }
}

/**
 * @description 字符串转换为md文件
 * @param { string } string 传入的字符串
 * @param { string } _name 文件名，不传的话默认为defaultName
 * @return { File } 返回转换后的文件对象
 */
export function stringToMd(string: string, _name?: string): File {
  const name = _name?.split(".")[0] || "defaultName";
  const blob = new Blob([string], { type: "text/plain" });
  const file = new File([blob], `${name}.md`, { type: "text/plain" });
  return file;
}

/**
 * @description
 * @param {  }
 * @return {  }
 */
export async function getFingerPrint(): Promise<string> {
  // @ts-ignore
  return import("./fingerprintv4.js")
    .then((FingerprintJS) => FingerprintJS.load())
    .then((fp: any) => fp.get())
    .then((result: any) => {
      const visitorId = result.visitorId;
      return visitorId;
    });
}
